/**
 * Copyright 2012 Alfredo "Rainbowbreeze" Morresi
 * 
 * This file is part of Apps4Italy PM10 project.
 * 
 * PM10 is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * PM10 is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with Foobar. If not, see http://www.gnu.org/licenses/.
 */
package it.rainbowbreeze.eureka.ui.view;

import it.rainbowbreeze.eureka.R;
import it.rainbowbreeze.eureka.common.AppEnv;
import it.rainbowbreeze.eureka.common.LogFacility;

import java.util.ArrayList;
import java.util.Collection;

import android.app.AlertDialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

/**
 * Draw on the map a circular overlay, that represent the quality of life
 * in the given point and surrounding area
 * 
 * @author Alfredo "Rainbowbreeze" Morresi
 */
public class QualityItemizedOverlay extends ItemizedOverlay<QualityOverlayItem> {
    // ------------------------------------------ Private Fields
    private final static String LOG_HASH = QualityItemizedOverlay.class.getSimpleName();
    
    private final LogFacility mLogFacility;
    private ArrayList<QualityOverlayItem> mOverlays = new ArrayList<QualityOverlayItem>();
    private final Context mContext;
    
    private final Paint[] mPaints;
    
    
    // -------------------------------------------- Constructors
    public QualityItemizedOverlay(
            LogFacility logFacility,
            Drawable marker,
            Context context) {
        super(boundCenterBottom(marker));
        mLogFacility = logFacility;
        mContext = context;
        mPaints = new Paint[AppEnv.MAX_LEVEL];
        initializePaintColors(context);
        populate(); //otherwise, a NPE is thrown if no points are present
    }
    
    // --------------------------------------- Public Properties

    // -------------------------------------------------- Events
    @Override
    protected boolean onTap(int index) {
        if (index >= mOverlays.size()) {
            //strange, but it could happend
            return true;
        }
        OverlayItem item = mOverlays.get(index);
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        dialog.show();
        return true;
    }
    
    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
        
        if (null != mOverlays) {
            canvas.save();
            
            Point point = null;
            Projection projection = mapView.getProjection();
            for (QualityOverlayItem overlayItem : mOverlays) {
                point = projection.toPixels(overlayItem.getPoint(), point);
//                mLogFacility.e(LOG_HASH, "point x:" + point.x + ", y:" + point.y);
                int radius = (int) projection.metersToEquatorPixels(overlayItem.getRadius());
//                mLogFacility.e(LOG_HASH,"radius: " + radius + ", original: " + overlayItem.getRadius());
                int paintIndex = overlayItem.getQuality() < AppEnv.MAX_LEVEL
                        ? overlayItem.getQuality() 
                        : AppEnv.MAX_LEVEL - 1;
                        
                canvas.drawCircle(
                        point.x,
                        point.y,
                        radius,
                        mPaints[paintIndex]);
            }
            
            canvas.restore();
        }
        
        return false;
        //return super.draw(canvas, mapView, shadow, when);
    }
    
    
    // ------------------------------------------ Public Methods
    /* (non-Javadoc)
     * @see com.google.android.maps.ItemizedOverlay#createItem(int)
     */
    @Override
    protected QualityOverlayItem createItem(int i) {
        return mOverlays.get(i);
    }

    /* (non-Javadoc)
     * @see com.google.android.maps.ItemizedOverlay#size()
     */
    @Override
    public int size() {
        return mOverlays.size();
    }
    
    
    /**
     * Adds a new point to the overlay
     * @param overlay
     */
    public void addOverlay(QualityOverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }
    
    public void addAllOverlays(Collection<QualityItemizedOverlay> overlays) {
        mOverlays.addAll(mOverlays);
        populate();
    }
    
    /**
     * Clears all stored overlays
     */
    public void clear() {
        mOverlays.clear();
        //TODO need this call?
        //populate();
    }
    
    // ----------------------------------------- Private Methods
    private void initializePaintColors(Context context) {
        Resources res = context.getResources();
        mPaints[0] = new Paint();
        mPaints[0].setColor(res.getColor(R.color.common_quality1));
        mPaints[1] = new Paint();
        mPaints[1].setColor(res.getColor(R.color.common_quality2));
        mPaints[2] = new Paint();
        mPaints[2].setColor(res.getColor(R.color.common_quality3));
        mPaints[3] = new Paint();
        mPaints[3].setColor(res.getColor(R.color.common_quality4));
        mPaints[4] = new Paint();
        mPaints[4].setColor(res.getColor(R.color.common_quality5));
    }


    // ----------------------------------------- Private Classes
}
